var userData={
    userName:null,
    password:null,
    initialized:false
}

var UIcontroller={
    onConfiguration:function(){
        
        window.openDialog("chrome://pennytel/content/config.xul", "ap_wndobj", "centerscreen,modal");
    },
    onCompose:function(){
        window.openDialog("chrome://pennytel/content/compose.xul", "ap_wndobj", "centerscreen,modal");
    },
    onAbout:function(){
        window.openDialog("chrome://pennytel/content/about.xul", "ap_wndobj", "centerscreen,modal");
    }
}



var gController={
	UserDataFile:"pennytel.ini",
	
	initialize:function(){
		gController.loadUserData();
	},
	loadUserData:function() {
	
        var savefile = gController.getProfileFolder()+gController.UserDataFile;
        
        try {
		    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	    } catch (e) {
            Components.utils.reportError("Permission to read file was denied.");
        }
        
        var file = Components.classes["@mozilla.org/file/local;1"]
            .createInstance(Components.interfaces.nsILocalFile);
        file.initWithPath( savefile );
        if (file.exists()) {
			var is = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
			is.init(file, 0x01, 00004, null);
			var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
			sis.init(is);
			var output = sis.read(sis.available());
			var outputArray = output.split(",");
			if (outputArray.length == 2) {
				userData.userName = outputArray[0];
				userData.password = outputArray[1];
				userData.initialized = true;
			}
		}
	},
    getProfileFolder:function() {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
            Components.utils.reportError("Permission to save file was denied.");
        }
        // get the path to the user's home (profile) directory
        const DIR_SERVICE = new Components.Constructor("@mozilla.org/file/directory_service;1","nsIProperties");
        try { 
            path=(new DIR_SERVICE()).get("ProfD", Components.interfaces.nsIFile).path; 
        } catch (e) {
            Components.utils.reportError("Error getting the profile");
        }
        // determine the file-separator
        if (path.search(/\\/) != -1) {
            path = path + "\\";
        } else {
            path = path + "/";
        }
        return path;
    },
	writeUserData:function () {
        var savefile = gController.getProfileFolder()+gController.UserDataFile;
        
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
            Components.utils.reportError("Permission to save file was denied.");
        }
        var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
        file.initWithPath( savefile );
        
        if ( file.exists() == false ) {
            file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
        }
        
        var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance( Components.interfaces.nsIFileOutputStream );
       
        outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
        
        var output = userData.userName+","+userData.password;
        var result = outputStream.write( output, output.length );
        outputStream.close();
        
	}
}

gController.initialize();